<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>019-Android自定义View(14)：手势处理库android-gesture-detectors详解(图文)</title>
  
  
  <meta property="og:title" content="019-Android自定义View(14)：手势处理库android-gesture-detectors详解(图文)">
  
  
  <meta property="og:url" content="http://example.com/2019/08/12/019-Android%E8%87%AA%E5%AE%9A%E4%B9%89View(14)%EF%BC%9A%E6%89%8B%E5%8A%BF%E5%A4%84%E7%90%86%E5%BA%93android-gesture-detectors%E8%AF%A6%E8%A7%A3(%E5%9B%BE%E6%96%87)/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-08-12">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android自定义View">
  
  <meta property="og:article:tag" content="Android事件">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      019-Android自定义View(14)：手势处理库android-gesture-detectors详解(图文)
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-08-12T04:00:32.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-08-12</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>2.2k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android自定义View/" class="post-meta-link">Android自定义View</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android事件/" class="post-meta-link">Android事件</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Cupster/article/details/113352077">【5年Android从零复盘系列之十九】Android自定义View(14)：手势处理库android-gesture-detectors</a></p>
<hr />


<h1 id="1-简介"><a href="#1-简介" class="headerlink" title="1.简介"></a>1.简介</h1><p><a target="_blank" rel="noopener" href="https://github.com/Almeros/android-gesture-detectors">android-gesture-detectors</a>是作者<a target="_blank" rel="noopener" href="https://github.com/Almeros">almeros</a>封装的一个手势处理库，可以直接使用处理两指旋转手势、推压手势、拉伸手势、拖拽手势，以及Android自带的缩放手势。</p>
<p>源码简单，量不多，有兴趣可以自行阅读源码，会有不小收获。</p>
<p><a target="_blank" rel="noopener" href="https://github.com/Almeros/android-gesture-detectors">Github连接</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/Almeros/android-gesture-detectors">https://github.com/Almeros/android-gesture-detectors</a></p>
<br/>

<p>也可运行代码直接运行测试</p>
<p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/14950791">文章示例代码</a></p>
<h2 id="1-1-双指旋转"><a href="#1-1-双指旋转" class="headerlink" title="1.1 双指旋转"></a>1.1 双指旋转</h2><img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128161701_ball_rotate.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128161701_ball_rotate.gif" class="lozad post-image">

<h2 id="1-2-单指拖拽"><a href="#1-2-单指拖拽" class="headerlink" title="1.2 单指拖拽"></a>1.2 单指拖拽</h2><img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128161801_ball_move.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128161801_ball_move.gif" class="lozad post-image">

<h2 id="1-3-双指推压、双指拉取"><a href="#1-3-双指推压、双指拉取" class="headerlink" title="1.3 双指推压、双指拉取"></a>1.3 双指推压、双指拉取</h2><img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128163825_press_push.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128163825_press_push.gif" class="lozad post-image">

<h2 id="1-4-双指缩放"><a href="#1-4-双指缩放" class="headerlink" title="1.4 双指缩放"></a>1.4 双指缩放</h2><img src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128163955_ball_scale.gif" / srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://gitee.com/cupster/picbed/raw/master/uPic/20210128163955_ball_scale.gif" class="lozad post-image">


<h1 id="2-准备工作"><a href="#2-准备工作" class="headerlink" title="2.准备工作"></a>2.准备工作</h1><p>也可直接参考项目代码<br><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/14950791">文章示例代码</a></p>
<h2 id="2-1-step1-在项目级build-gradle中添加jitpack仓库"><a href="#2-1-step1-在项目级build-gradle中添加jitpack仓库" class="headerlink" title="2.1 step1:在项目级build.gradle中添加jitpack仓库"></a>2.1 step1:在项目级build.gradle中添加jitpack仓库</h2><pre><code>//step-1.1:添加jitpack仓库
maven &#123; url &#39;https://jitpack.io&#39; &#125;
</code></pre>
<p>两个repositories闭包均添加，如：</p>
<pre class="highlight"><span class="line"><span class="keyword">buildscript</span> &#123;</span><br><span class="line">    <span class="keyword">repositories</span> &#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">//step-1.1:添加jitpack仓库</span></span><br><span class="line">    maven &#123; url <span class="string">&#x27;https://jitpack.io&#x27;</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">allprojects</span> &#123;</span><br><span class="line">    <span class="keyword">repositories</span> &#123;</span><br><span class="line">        google()</span><br><span class="line">        jcenter()</span><br><span class="line">        <span class="comment">//step-1.2:添加jitpack仓库</span></span><br><span class="line">        maven &#123; url <span class="string">&#x27;https://jitpack.io&#x27;</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h2 id="2-2-step2-moudule的build-gradle导包"><a href="#2-2-step2-moudule的build-gradle导包" class="headerlink" title="2.2 step2:moudule的build.gradle导包"></a>2.2 step2:moudule的build.gradle导包</h2><pre><code>//step-2:导包
implementation &#39;com.github.Almeros:android-gesture-detectors:v1.0.1&#39;
</code></pre>
<h2 id="2-3-step3-布局代码"><a href="#2-3-step3-布局代码" class="headerlink" title="2.3 step3:布局代码"></a>2.3 step3:布局代码</h2><p>res/layout/activity_rotate.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">RelativeLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span>&gt;</span></span><br><span class="line">    &lt;ImageView</span><br><span class="line">        android:id=&quot;@+id/image&quot;</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;match_parent&quot;</span><br><span class="line">        android:scaleType=&quot;matrix&quot;</span><br><span class="line">        android:src=&quot;@mipmap/ic_football&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">RelativeLayout</span>&gt;</span></span><br></pre>


<h1 id="3-旋转手势"><a href="#3-旋转手势" class="headerlink" title="3.旋转手势"></a>3.旋转手势</h1><p><strong>用法&amp;思路：</strong></p>
<ol>
<li>声明RotateGestureDetector对象，初始化，设置监听器等；</li>
<li>获取操作对象如ImageView对象，设置setOnTouchListener；</li>
<li>在OnTouchListener的onTouch回调中，RotateGestureDetector实例对象接手触摸事件；</li>
<li>旋转回调中，设置变化后的参数值；</li>
<li>ontouch()回调中处理变化结果，并设置给操作对象如ImageView</li>
</ol>
<p><strong>代码及要点说明：</strong></p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.easydetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.graphics.Matrix;</span><br><span class="line"><span class="keyword">import</span> android.graphics.PointF;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.view.MotionEvent;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.ImageView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.MoveGestureDetector;</span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.RotateGestureDetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">RotateActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnTouchListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusX = <span class="number">0.0f</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusY = <span class="number">0.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mImageHeight, mImageWidth;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Matrix mMatrix = <span class="keyword">new</span> Matrix();</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mRotationDegrees = <span class="number">0.0f</span>;<span class="comment">//旋转角度</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mScaleFactor = <span class="number">0.5f</span>;<span class="comment">//初始缩放系数</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> RotateGestureDetector mRotateDetector;</span><br><span class="line">    <span class="keyword">private</span> MoveGestureDetector mMoveDetector;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(<span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_rotate);</span><br><span class="line">        ImageView img = (ImageView) findViewById(R.id.image);</span><br><span class="line">        img.setOnTouchListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        mFocusX = getResources().getDisplayMetrics().widthPixels/<span class="number">2f</span>;</span><br><span class="line">        mFocusY = getResources().getDisplayMetrics().heightPixels/<span class="number">2f</span>;</span><br><span class="line"></span><br><span class="line">        mImageWidth = img.getDrawable().getIntrinsicWidth();</span><br><span class="line">        mImageHeight = img.getDrawable().getIntrinsicHeight();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterX = (mImageWidth*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterY = (mImageHeight*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始缩放、位置居中</span></span><br><span class="line">        mMatrix.postScale(mScaleFactor, mScaleFactor);</span><br><span class="line">        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);</span><br><span class="line">        img.setImageMatrix(mMatrix);</span><br><span class="line"></span><br><span class="line">        mRotateDetector = <span class="keyword">new</span> RotateGestureDetector(getApplicationContext(), <span class="keyword">new</span> RotateGestureDetector.SimpleOnRotateGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onRotate</span><span class="params">(RotateGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                mRotationDegrees -= detector.getRotationDegreesDelta();</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        mMoveDetector 	= <span class="keyword">new</span> MoveGestureDetector(getApplicationContext(), <span class="keyword">new</span> MoveGestureDetector.SimpleOnMoveGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onMove</span><span class="params">(MoveGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                PointF d = detector.getFocusDelta();</span><br><span class="line">                mFocusX += d.x;</span><br><span class="line">                mFocusY += d.y;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouch</span><span class="params">(View view, MotionEvent motionEvent)</span> </span>&#123;</span><br><span class="line">        mRotateDetector.onTouchEvent(motionEvent);</span><br><span class="line">        mMoveDetector.onTouchEvent(motionEvent);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterX = (mImageWidth*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterY = (mImageHeight*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        mMatrix.reset();</span><br><span class="line">        mMatrix.postRotate(mRotationDegrees,  scaledImageCenterX, scaledImageCenterY);</span><br><span class="line">        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);</span><br><span class="line"></span><br><span class="line">        ( (ImageView) view).setImageMatrix(mMatrix);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">//true:消费掉事件，不继续分发</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<h1 id="4-拖拽手势"><a href="#4-拖拽手势" class="headerlink" title="4.拖拽手势"></a>4.拖拽手势</h1><p><strong>用法&amp;思路：</strong></p>
<ol>
<li>声明MoveGestureDetector对象，初始化，设置监听器等；</li>
<li>获取操作对象如ImageView对象，设置setOnTouchListener；</li>
<li>在OnTouchListener的onTouch回调中，MoveGestureDetector实例对象接手触摸事件；</li>
<li>旋转回调中，设置变化后的参数值；</li>
<li>ontouch()回调中处理变化结果，并设置给操作对象如ImageView</li>
</ol>
<p><strong>代码及要点说明：</strong></p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.easydetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.graphics.Matrix;</span><br><span class="line"><span class="keyword">import</span> android.graphics.PointF;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.view.MotionEvent;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.ImageView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.MoveGestureDetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">MoveActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnTouchListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusX = <span class="number">0.0f</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusY = <span class="number">0.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mImageHeight, mImageWidth;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Matrix mMatrix = <span class="keyword">new</span> Matrix();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> MoveGestureDetector mMoveDetector;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(<span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_rotate);</span><br><span class="line">        ImageView img = (ImageView) findViewById(R.id.image);</span><br><span class="line">        img.setOnTouchListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        mFocusX = getResources().getDisplayMetrics().widthPixels/<span class="number">2f</span>;</span><br><span class="line">        mFocusY = getResources().getDisplayMetrics().heightPixels/<span class="number">2f</span>;</span><br><span class="line"></span><br><span class="line">        mImageWidth = img.getDrawable().getIntrinsicWidth();</span><br><span class="line">        mImageHeight = img.getDrawable().getIntrinsicHeight();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始位置居中</span></span><br><span class="line">        mMatrix.postTranslate(mFocusX - mImageWidth/<span class="number">2f</span>, mFocusY - mImageHeight/<span class="number">2f</span>);</span><br><span class="line">        img.setImageMatrix(mMatrix);</span><br><span class="line"></span><br><span class="line">        mMoveDetector 	= <span class="keyword">new</span> MoveGestureDetector(getApplicationContext(), <span class="keyword">new</span> MoveGestureDetector.SimpleOnMoveGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onMove</span><span class="params">(MoveGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                PointF d = detector.getFocusDelta();</span><br><span class="line">                mFocusX += d.x;</span><br><span class="line">                mFocusY += d.y;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouch</span><span class="params">(View view, MotionEvent motionEvent)</span> </span>&#123;</span><br><span class="line">        mMoveDetector.onTouchEvent(motionEvent);</span><br><span class="line"></span><br><span class="line">        mMatrix.reset();</span><br><span class="line">        mMatrix.postTranslate(mFocusX - mImageWidth/<span class="number">2f</span>, mFocusY - mImageHeight/<span class="number">2f</span>);</span><br><span class="line"></span><br><span class="line">        ( (ImageView) view).setImageMatrix(mMatrix);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">//true:消费掉事件，不继续分发</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>


<h1 id="5-双指推拉手势"><a href="#5-双指推拉手势" class="headerlink" title="5.双指推拉手势"></a>5.双指推拉手势</h1><p><strong>用法&amp;思路：</strong></p>
<ol>
<li>声明ShoveGestureDetector对象，初始化，设置监听器等；</li>
<li>获取操作对象如ImageView对象，设置setOnTouchListener；</li>
<li>在OnTouchListener的onTouch回调中，ShoveGestureDetector实例对象接手触摸事件；</li>
<li>旋转回调中，设置变化后的参数值；</li>
<li>ontouch()回调中处理变化结果，并设置给操作对象如ImageView</li>
</ol>
<p><strong>代码及要点说明：</strong></p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.easydetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.graphics.Matrix;</span><br><span class="line"><span class="keyword">import</span> android.graphics.PointF;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.view.MotionEvent;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.ImageView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.MoveGestureDetector;</span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.ShoveGestureDetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">PressPushActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnTouchListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusX = <span class="number">0.0f</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusY = <span class="number">0.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mImageHeight, mImageWidth;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mAlpha = <span class="number">255</span>;</span><br><span class="line">    <span class="keyword">private</span> Matrix mMatrix = <span class="keyword">new</span> Matrix();</span><br><span class="line">    <span class="keyword">private</span> MoveGestureDetector mMoveDetector;</span><br><span class="line">    <span class="keyword">private</span> ShoveGestureDetector mShoveDetector;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(<span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_rotate);</span><br><span class="line">        ImageView img = (ImageView) findViewById(R.id.image);</span><br><span class="line">        img.setOnTouchListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        mFocusX = getResources().getDisplayMetrics().widthPixels/<span class="number">2f</span>;</span><br><span class="line">        mFocusY = getResources().getDisplayMetrics().heightPixels/<span class="number">2f</span>;</span><br><span class="line"></span><br><span class="line">        mImageWidth = img.getDrawable().getIntrinsicWidth();</span><br><span class="line">        mImageHeight = img.getDrawable().getIntrinsicHeight();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始缩放、位置居中</span></span><br><span class="line">        mMatrix.postTranslate(mFocusX - mImageWidth/<span class="number">2f</span>, mFocusY - mImageHeight/<span class="number">2f</span>);</span><br><span class="line">        img.setImageMatrix(mMatrix);</span><br><span class="line"></span><br><span class="line">        mMoveDetector 	= <span class="keyword">new</span> MoveGestureDetector(getApplicationContext(), <span class="keyword">new</span> MoveGestureDetector.SimpleOnMoveGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onMove</span><span class="params">(MoveGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                PointF d = detector.getFocusDelta();</span><br><span class="line">                mFocusX += d.x;</span><br><span class="line">                mFocusY += d.y;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        mShoveDetector 	= <span class="keyword">new</span> ShoveGestureDetector(getApplicationContext(), <span class="keyword">new</span> ShoveGestureDetector.SimpleOnShoveGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onShove</span><span class="params">(ShoveGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                mAlpha += detector.getShovePixelsDelta();</span><br><span class="line">                <span class="keyword">if</span> (mAlpha &gt; <span class="number">255</span>)&#123;</span><br><span class="line">                    mAlpha = <span class="number">255</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (mAlpha &lt; <span class="number">0</span>)&#123;</span><br><span class="line">                    mAlpha = <span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouch</span><span class="params">(View view, MotionEvent motionEvent)</span> </span>&#123;</span><br><span class="line">        mMoveDetector.onTouchEvent(motionEvent);</span><br><span class="line">        mShoveDetector.onTouchEvent(motionEvent);</span><br><span class="line"></span><br><span class="line">        mMatrix.reset();</span><br><span class="line">        mMatrix.postTranslate(mFocusX - mImageWidth/<span class="number">2f</span>, mFocusY - mImageHeight/<span class="number">2f</span>);</span><br><span class="line"></span><br><span class="line">        ( (ImageView) view).setImageMatrix(mMatrix);</span><br><span class="line">        ( (ImageView) view).getDrawable().setAlpha(mAlpha);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">//true:消费掉事件，不继续分发</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h1 id="5-缩放手势"><a href="#5-缩放手势" class="headerlink" title="5.缩放手势"></a>5.缩放手势</h1><p><strong>用法&amp;思路：</strong></p>
<ol>
<li>声明ScaleGestureDetector对象，初始化，设置监听器等；</li>
<li>获取操作对象如ImageView对象，设置setOnTouchListener；</li>
<li>在OnTouchListener的onTouch回调中，ScaleGestureDetector实例对象接手触摸事件；</li>
<li>旋转回调中，设置变化后的参数值；</li>
<li>ontouch()回调中处理变化结果，并设置给操作对象如ImageView</li>
</ol>
<p><strong>代码及要点说明：</strong></p>
<pre class="highlight"><span class="line"><span class="keyword">package</span> com.cupster.easydetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.graphics.Matrix;</span><br><span class="line"><span class="keyword">import</span> android.graphics.PointF;</span><br><span class="line"><span class="keyword">import</span> android.os.Bundle;</span><br><span class="line"><span class="keyword">import</span> android.view.MotionEvent;</span><br><span class="line"><span class="keyword">import</span> android.view.ScaleGestureDetector;</span><br><span class="line"><span class="keyword">import</span> android.view.View;</span><br><span class="line"><span class="keyword">import</span> android.widget.ImageView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.MoveGestureDetector;</span><br><span class="line"><span class="keyword">import</span> com.almeros.android.multitouch.RotateGestureDetector;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> androidx.annotation.Nullable;</span><br><span class="line"><span class="keyword">import</span> androidx.appcompat.app.AppCompatActivity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>  <span class="class"><span class="keyword">class</span> <span class="title">ScaleActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnTouchListener</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusX = <span class="number">0.0f</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mFocusY = <span class="number">0.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> mImageHeight, mImageWidth;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Matrix mMatrix = <span class="keyword">new</span> Matrix();</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mScaleFactor = <span class="number">0.5f</span>;<span class="comment">//初始缩放系数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mMinScale = <span class="number">0.2f</span>;<span class="comment">//min缩放系数</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">float</span> mMaxScale = <span class="number">10.0f</span>;<span class="comment">//max缩放系数</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ScaleGestureDetector mScaleDetector;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(<span class="meta">@Nullable</span> Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_rotate);</span><br><span class="line">        ImageView img = (ImageView) findViewById(R.id.image);</span><br><span class="line">        img.setOnTouchListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        mFocusX = getResources().getDisplayMetrics().widthPixels/<span class="number">2f</span>;</span><br><span class="line">        mFocusY = getResources().getDisplayMetrics().heightPixels/<span class="number">2f</span>;</span><br><span class="line"></span><br><span class="line">        mImageWidth = img.getDrawable().getIntrinsicWidth();</span><br><span class="line">        mImageHeight = img.getDrawable().getIntrinsicHeight();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterX = (mImageWidth*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterY = (mImageHeight*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始缩放、位置居中</span></span><br><span class="line">        mMatrix.postScale(mScaleFactor, mScaleFactor);</span><br><span class="line">        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);</span><br><span class="line">        img.setImageMatrix(mMatrix);</span><br><span class="line"></span><br><span class="line">        mScaleDetector 	= <span class="keyword">new</span> ScaleGestureDetector(getApplicationContext(), <span class="keyword">new</span> ScaleGestureDetector.SimpleOnScaleGestureListener()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onScale</span><span class="params">(ScaleGestureDetector detector)</span> </span>&#123;</span><br><span class="line">                mScaleFactor *= detector.getScaleFactor();</span><br><span class="line">                mScaleFactor = Math.max(mMinScale, Math.min(mScaleFactor, mMaxScale));</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouch</span><span class="params">(View view, MotionEvent motionEvent)</span> </span>&#123;</span><br><span class="line">        mScaleDetector.onTouchEvent(motionEvent);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterX = (mImageWidth*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">float</span> scaledImageCenterY = (mImageHeight*mScaleFactor)/<span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">        mMatrix.reset();</span><br><span class="line">        mMatrix.postScale(mScaleFactor, mScaleFactor);</span><br><span class="line">        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);</span><br><span class="line"></span><br><span class="line">        ( (ImageView) view).setImageMatrix(mMatrix);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;<span class="comment">//true:消费掉事件，不继续分发</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>

<h1 id="6-其他次要代码"><a href="#6-其他次要代码" class="headerlink" title="6. 其他次要代码"></a>6. 其他次要代码</h1><p><a target="_blank" rel="noopener" href="https://download.csdn.net/download/Cupster/14950791">代码.zip</a></p>
<p>activity_main.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:app</span>=<span class="string">&quot;http://schemas.android.com/apk/res-auto&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">&quot;http://schemas.android.com/tools&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">&quot;match_parent&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:gravity</span>=<span class="string">&quot;center&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:orientation</span>=<span class="string">&quot;vertical&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_rotate&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:minHeight=&quot;0dp&quot;</span><br><span class="line">        android:text=&quot;旋转&quot;</span><br><span class="line">        android:textAllCaps=&quot;false&quot; /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_pull&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:minHeight=&quot;0dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">        android:text=&quot;拖拽&quot;</span><br><span class="line">        android:textAllCaps=&quot;false&quot; /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_press_push&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:minHeight=&quot;0dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">        android:text=&quot;推压&quot;</span><br><span class="line">        android:textAllCaps=&quot;false&quot; /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:id=&quot;@+id/btn_scale&quot;</span><br><span class="line">        android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        android:minHeight=&quot;0dp&quot;</span><br><span class="line">        android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">        android:text=&quot;缩放&quot;</span><br><span class="line">        android:textAllCaps=&quot;false&quot; /&gt;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></span><br></pre>

<p>AndroidManifest.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">manifest</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">package</span>=<span class="string">&quot;com.cupster.easydetector&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    &lt;application</span><br><span class="line">        android:allowBackup=&quot;true&quot;</span><br><span class="line">        android:icon=&quot;@mipmap/ic_launcher&quot;</span><br><span class="line">        android:label=&quot;@string/app_name&quot;</span><br><span class="line">        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;</span><br><span class="line">        android:supportsRtl=&quot;true&quot;</span><br><span class="line">        android:theme=&quot;@style/AppTheme&quot;&gt;</span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.RotateActivity&quot;</span> <span class="attr">android:screenOrientation</span>=<span class="string">&quot;portrait&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.MoveActivity&quot;</span> <span class="attr">android:screenOrientation</span>=<span class="string">&quot;portrait&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.PressPushActivity&quot;</span> <span class="attr">android:screenOrientation</span>=<span class="string">&quot;portrait&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.ScaleActivity&quot;</span> <span class="attr">android:screenOrientation</span>=<span class="string">&quot;portrait&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">application</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">manifest</span>&gt;</span></span><br></pre>


  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/08/12/019-Android%E8%87%AA%E5%AE%9A%E4%B9%89View(14)%EF%BC%9A%E6%89%8B%E5%8A%BF%E5%A4%84%E7%90%86%E5%BA%93android-gesture-detectors%E8%AF%A6%E8%A7%A3(%E5%9B%BE%E6%96%87)/">http://example.com/2019/08/12/019-Android%E8%87%AA%E5%AE%9A%E4%B9%89View(14)%EF%BC%9A%E6%89%8B%E5%8A%BF%E5%A4%84%E7%90%86%E5%BA%93android-gesture-detectors%E8%AF%A6%E8%A7%A3(%E5%9B%BE%E6%96%87)/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/08/16/020-Android自定义View(15)：Matrix详解(图文)/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">020-Android自定义View(15)：Matrix详解(图文) </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/08/08/018-Android自定义View(13)：自定义手势存储及使用详解(图文)/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">018-Android自定义View(13)：自定义手势存储及使用详解(图文) </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="toc-text">1.简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E5%8F%8C%E6%8C%87%E6%97%8B%E8%BD%AC"><span class="toc-text">1.1 双指旋转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E5%8D%95%E6%8C%87%E6%8B%96%E6%8B%BD"><span class="toc-text">1.2 单指拖拽</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E5%8F%8C%E6%8C%87%E6%8E%A8%E5%8E%8B%E3%80%81%E5%8F%8C%E6%8C%87%E6%8B%89%E5%8F%96"><span class="toc-text">1.3 双指推压、双指拉取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-%E5%8F%8C%E6%8C%87%E7%BC%A9%E6%94%BE"><span class="toc-text">1.4 双指缩放</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-text">2.准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-step1-%E5%9C%A8%E9%A1%B9%E7%9B%AE%E7%BA%A7build-gradle%E4%B8%AD%E6%B7%BB%E5%8A%A0jitpack%E4%BB%93%E5%BA%93"><span class="toc-text">2.1 step1:在项目级build.gradle中添加jitpack仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-step2-moudule%E7%9A%84build-gradle%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 step2:moudule的build.gradle导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-step3-%E5%B8%83%E5%B1%80%E4%BB%A3%E7%A0%81"><span class="toc-text">2.3 step3:布局代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E6%97%8B%E8%BD%AC%E6%89%8B%E5%8A%BF"><span class="toc-text">3.旋转手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%8B%96%E6%8B%BD%E6%89%8B%E5%8A%BF"><span class="toc-text">4.拖拽手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%8F%8C%E6%8C%87%E6%8E%A8%E6%8B%89%E6%89%8B%E5%8A%BF"><span class="toc-text">5.双指推拉手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E7%BC%A9%E6%94%BE%E6%89%8B%E5%8A%BF"><span class="toc-text">5.缩放手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">6. 其他次要代码</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="toc-text">1.简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E5%8F%8C%E6%8C%87%E6%97%8B%E8%BD%AC"><span class="toc-text">1.1 双指旋转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E5%8D%95%E6%8C%87%E6%8B%96%E6%8B%BD"><span class="toc-text">1.2 单指拖拽</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E5%8F%8C%E6%8C%87%E6%8E%A8%E5%8E%8B%E3%80%81%E5%8F%8C%E6%8C%87%E6%8B%89%E5%8F%96"><span class="toc-text">1.3 双指推压、双指拉取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-%E5%8F%8C%E6%8C%87%E7%BC%A9%E6%94%BE"><span class="toc-text">1.4 双指缩放</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-text">2.准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-step1-%E5%9C%A8%E9%A1%B9%E7%9B%AE%E7%BA%A7build-gradle%E4%B8%AD%E6%B7%BB%E5%8A%A0jitpack%E4%BB%93%E5%BA%93"><span class="toc-text">2.1 step1:在项目级build.gradle中添加jitpack仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-step2-moudule%E7%9A%84build-gradle%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 step2:moudule的build.gradle导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-step3-%E5%B8%83%E5%B1%80%E4%BB%A3%E7%A0%81"><span class="toc-text">2.3 step3:布局代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E6%97%8B%E8%BD%AC%E6%89%8B%E5%8A%BF"><span class="toc-text">3.旋转手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%8B%96%E6%8B%BD%E6%89%8B%E5%8A%BF"><span class="toc-text">4.拖拽手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%8F%8C%E6%8C%87%E6%8E%A8%E6%8B%89%E6%89%8B%E5%8A%BF"><span class="toc-text">5.双指推拉手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E7%BC%A9%E6%94%BE%E6%89%8B%E5%8A%BF"><span class="toc-text">5.缩放手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">6. 其他次要代码</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="toc-text">1.简介</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-%E5%8F%8C%E6%8C%87%E6%97%8B%E8%BD%AC"><span class="toc-text">1.1 双指旋转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-%E5%8D%95%E6%8C%87%E6%8B%96%E6%8B%BD"><span class="toc-text">1.2 单指拖拽</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-%E5%8F%8C%E6%8C%87%E6%8E%A8%E5%8E%8B%E3%80%81%E5%8F%8C%E6%8C%87%E6%8B%89%E5%8F%96"><span class="toc-text">1.3 双指推压、双指拉取</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-%E5%8F%8C%E6%8C%87%E7%BC%A9%E6%94%BE"><span class="toc-text">1.4 双指缩放</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-text">2.准备工作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-step1-%E5%9C%A8%E9%A1%B9%E7%9B%AE%E7%BA%A7build-gradle%E4%B8%AD%E6%B7%BB%E5%8A%A0jitpack%E4%BB%93%E5%BA%93"><span class="toc-text">2.1 step1:在项目级build.gradle中添加jitpack仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-step2-moudule%E7%9A%84build-gradle%E5%AF%BC%E5%8C%85"><span class="toc-text">2.2 step2:moudule的build.gradle导包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-step3-%E5%B8%83%E5%B1%80%E4%BB%A3%E7%A0%81"><span class="toc-text">2.3 step3:布局代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E6%97%8B%E8%BD%AC%E6%89%8B%E5%8A%BF"><span class="toc-text">3.旋转手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%8B%96%E6%8B%BD%E6%89%8B%E5%8A%BF"><span class="toc-text">4.拖拽手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%8F%8C%E6%8C%87%E6%8E%A8%E6%8B%89%E6%89%8B%E5%8A%BF"><span class="toc-text">5.双指推拉手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E7%BC%A9%E6%94%BE%E6%89%8B%E5%8A%BF"><span class="toc-text">5.缩放手势</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%85%B6%E4%BB%96%E6%AC%A1%E8%A6%81%E4%BB%A3%E7%A0%81"><span class="toc-text">6. 其他次要代码</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>